﻿#region MIT Licence
/*
 * Copyright (C) 2014 PdfCreative
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom 
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included 
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 
 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#endregion

using System;
using System.Collections.Generic;
using System.Text;

namespace PdfCreative.Pdf.Graphics.Tables
{
    /// <summary>
    /// The Row class represents a single row in a table. A row contains a number of cells, and
    /// optionally specifies a fixed row height.
    /// </summary>
    public class Row
    {
        // Private fields
        // ==============
        #region Private fields
        private List<Cell> _cells;                  // The cells in this row.
        private float? _height;                     // The row height.
        #endregion



        // Public properties
        // =================
        #region Public properties
        /// <summary>
        /// Retrieves the cells in this row.
        /// </summary>
        public List<Cell> Cells
        {
            get { return _cells; }
        }

        /// <summary>
        /// Sets or retrieves the row's optional height. This is a nullable field.
        /// When Height is set to a null reference, the row height is determined by
        /// cell content.
        /// For fixed height, the value must be non-negative.
        /// </summary>
        public float? Height
        {
            get { return _height; }
            set
            {
                if (value.HasValue && value.Value < 0)
                    throw new ArgumentOutOfRangeException(
                        "value", "Height must be zero or a positive number, or a null reference.");

                _height = value;
            }
        }
        #endregion



        // Public methods
        // ==============
        #region Public methods
        /// <summary>
        /// Adds a cell with the specified content to this row. This method returns the current
        /// row instance to allow chaining of statements.
        /// </summary>
        /// <param name="text">The content.</param>
        /// <param name="brush">The brush to draw the content with.</param>
        /// <param name="font">The font to draw the content in.</param>
        /// <param name="fontSize">The font size.</param>
        /// <returns>The current row instance.</returns>
        public Row Append(string text, Brush brush, Font font, float fontSize)
        {
            return AppendCell(new Cell(text, brush, font, fontSize));
        }

        /// <summary>
        /// Adds a cell with the specified styled content to this row. This method returns the current
        /// row instance to allow chaining of statements.
        /// </summary>
        /// <param name="content">The content.</param>
        /// <returns>The current row instance.</returns>
        public Row Append(StyledText content)
        {
            return AppendCell(new Cell(content));
        }

        /// <summary>
        /// Adds a cell to this row. This method returns the current row instance.
        /// </summary>
        /// <param name="cell">The cell.</param>
        /// <returns>The row instance.</returns>
        public Row AppendCell(Cell cell)
        {
            if (null == cell) throw new ArgumentNullException("cell");
            
            _cells.Add(cell);

            return this;
        }
        #endregion



        // Public constructor
        // ==================
        #region Public constructor
        /// <summary>
        /// Constructs a new Row instance.
        /// </summary>
        public Row() : this(null) { }

        /// <summary>
        /// Constructs a new Row instance with the specified height.
        /// </summary>
        /// <param name="height">The height of the row. For a row with a fixed height,
        /// specify a non-negative value. For a row with variable height,
        /// pass a null reference.</param>
        public Row(float? height)
        {
            _cells = new List<Cell>();
            this.Height = height;
        }
        #endregion

    } // Row class
}
